﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jxym.wlkc2011.Module.Default.model;
using System.Reflection;
using System.Data.SqlClient;
using Jxym.DBUtility;

namespace Jxym.wlkc2011.Module.Default.SERVER.User
{
    public partial class UserAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MsSqlDal<User_Info> iUi = new MsSqlDal<User_Info>();
                PropertyInfo[] fields = typeof(User_Info).GetProperties();
                List<string> listName = new List<string>();
                List<string> listValue = new List<string>();
                object identity = 0;
                int userid = 0;
                string roleids = "", returnStr = "{'success':false,'msg':'参数错误'}";

                foreach (PropertyInfo property in fields)
                {
                    if (Request.Params[property.Name] != null && property.Name != iUi.PrimaryKeyName)
                    {
                        listName.Add("@" + property.Name);
                        listValue.Add(Request.Params[property.Name]);
                    }
                }

                if (Request.Params[iUi.PrimaryKeyName] != null)
                {
                    userid = Convert.ToInt32(Request.Params[iUi.PrimaryKeyName]);
                    if (userid > 0)
                    {
                        listName.Add("@" + iUi.PrimaryKeyName);
                        listValue.Add(Request.Params[iUi.PrimaryKeyName]);
                    }
                }
                if (Request.Params["RoleID"] != null)
                {
                    roleids = Request.Params["RoleID"];
                }

                SqlParameter[] parameters = new SqlParameter[listName.Count];

                for (int i = 0; i < listName.Count; i++)
                {
                    parameters[i] = new SqlParameter(listName[i], listValue[i]);
                }

                if (userid != 0)
                {
                    identity = iUi.Updata(parameters);
                    UserUpRole(userid, roleids);
                    returnStr = "{'success':true,'msg':'更新完成'}";
                }
                else
                {
                    identity = iUi.Add(parameters);
                    UserUpRole(Convert.ToInt32(identity), roleids);
                    returnStr = "{'success':true,'msg':'添加完成'}";
                }

                Response.Write(returnStr);
            }
        }

        private void UserUpRole(int userId, string RoleIDs)
        {
            if (userId < 1) return;
            if (RoleIDs == "") return;

            MsSqlDal<User_RoleConfig> iUrc = new MsSqlDal<User_RoleConfig>();
            iUrc.Delete("UserID=" + userId, null);
            string[] ids = RoleIDs.Split(',');
            for (int i = 0; i < ids.Length; i++)
            {
                SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@UserID", userId), new SqlParameter("@RoleID", ids[i]) };
                iUrc.Add(parameters);
            }
        }
    }
}